<?php
require_once('./lib/config.php');

$hero = false;

if (!empty($_REQUEST['heroId']) && !empty($_COOKIE['realm']) && !empty($_COOKIE['battleTag'])) {
    $p = new Parser($_COOKIE['realm'], $_COOKIE['battleTag']);
    $data = $p->load();
    if (!empty($data['heroes'])) {
        foreach ($data['heroes'] as $h) {
            if ($h['id'] == $_REQUEST['heroId']) {
                $hero = $p->load(Parser::TYPE_HERO, $h['id']);
                if (!empty($hero['items'])) {
                    foreach ($hero['items'] as &$item) {
                        $item['data'] = $p->load(Parser::TYPE_ITEM, $item['tooltipParams']);
                    }
                }
            }
        }
    }
}

echo json_encode(array('ok' => $hero ? true : false, 'data' => $hero));